package com.api.shopapi.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.BusinessStaffRel;
import com.api.shopapi.entity.CommandCenterStaffRel;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.service.BusinessStaffRelService;
import com.api.shopapi.service.CommandCenterStaffRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;


/**
 * 指挥中心关联的后台账号
 * */

@RestController
@RequestMapping("CommandCenterStaffRel")
public class CommandCenterStaffRelController extends BaseController{

    @Autowired
    @Qualifier("CommandCenterStaffRelServiceImplOne")
    private CommandCenterStaffRelService commandCenterStaffRelService;


    //编辑
    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String commandCenterCode = jsonObject.getString("commandCenterCode");
            JSONArray listStaff = jsonObject.getJSONArray("listStaff");


            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/CommendCenterStaffRel/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            //删除老的关系
            List<CommandCenterStaffRel> listStaffRel = commandCenterStaffRelService.ListByCommandCenterCode(commandCenterCode);
            for(CommandCenterStaffRel commandCenterStaffRel : listStaffRel){
                commandCenterStaffRelService.delete(commandCenterStaffRel.getCommandCenterStaffRelCode());
            }

            //增加新的关系
            for (int k = 0; k < listStaff.size(); k++) {

                JSONObject staffTemp = listStaff.getJSONObject(k);

                CommandCenterStaffRel commandCenterStaffRel = new CommandCenterStaffRel();
                commandCenterStaffRel.setCommandCenterStaffRelCode(UUID.randomUUID().toString());
                commandCenterStaffRel.setCommandCenterCode(commandCenterCode);
                commandCenterStaffRel.setStaffCode(staffTemp.getString("staffCode"));
                commandCenterStaffRelService.insert(commandCenterStaffRel);
            }


            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/CommendCenterStaffRel/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


}
